<style scoped>
body {
  font-family: 'Open Sans', sans-serif;
  margin: 0;
  padding: 0;
  background-color: #f4f4f4;
}

#app {
  max-width: 800px;
  margin: 50px auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

h1 {
  text-align: center;
  color: #333;
}

.product-list {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  margin-top: 20px;
}

.product {
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 10px;
  text-align: center;
}

.product img {
  width: 100%;
  height: 150px;
  object-fit: contain;
  border-bottom: 1px solid #ddd;
}

.product h3 {
  margin: 0;
  padding: 5px;
  font-size: 16px;
  color: #333;
}

.product p {
  margin: 0;
  padding: 5px;
  font-size: 14px;
  color: #666;
}

.quantity {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 10px;
}

.quantity button {
  padding: 5px 10px;
  margin-right: 5px;
  background-color: #4caf50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.quantity button:hover {
  background-color: #45a049;
}

.quantity input {
  padding: 5px;
  text-align: center;
  width: 50px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.cart {
  margin-top: 20px;
}

.cart h3 {
  text-align: center;
}

.cart-items {
  display: flex;
  flex-direction: column;
  margin-top: 10px;
}

.cart-item {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  border-bottom: 1px solid #ddd;
}

.cart-item:last-child {
  border-bottom: none;
}

.checkout {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.checkout-btn {
  padding: 10px 20px;
  background-color: #2196f3;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.checkout-btn:hover {
  background-color: #0b7cda;
}
</style>

<template>
  <div class="test">
    <div id="app">
      <h1>Shopping Cart</h1>

      <!-- 商品列表 -->
      <div class="product-list">
        <div v-for="product in products" :key="product.id" class="product">
          <img :src="products.image" alt="Product Image">
          <h3>{{  product.name }}</h3>
          <h3>{{  product.price }}</h3>
          <div class="quantity"></div>

        </div>
      </div>

      <!-- 购物车 -->
      <div class="cart">
        <h3>Cart</h3>
        <div class="cart-items"></div>
        <div class="total-price"></div>
        <div class="checkout"></div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, reactive } from 'vue';

const products = reactive([
  {
    id: 1,
    name: 'Apple',
    price: 0.99,
    quantity: 0,
    image:
      'https://ts2.cn.mm.bing.net/th?id=OIP-C.-Y9W58cVwHMaH021cnmzGgHaIY&w=234&h=266&c=8&rs=1&qlt=90&o=6&cb=13&dpr=1.6&pid=3.1&rm=2'
  },
  {
    id: 2,
    name: 'Banana',
    price: 0.59,
    quantity: 0,
    image:
      'https://ts2.cn.mm.bing.net/th?id=OIP-C.JIE9eUSxFmiw15Du2ZC-XAHaGJ&w=274&h=227&c=8&rs=1&qlt=90&o=6&cb=13&dpr=1.6&pid=3.1&rm=2'
  },
  {
    id: 3,
    name: 'Orange',
    price: 0.79,
    quantity: 0,
    image:
      'https://ts2.cn.mm.bing.net/th?id=OIP-C.7DUeKG3rK4bwCv7Yei1--wHaGB&w=277&h=225&c=8&rs=1&qlt=90&o=6&cb=13&dpr=1.6&pid=3.1&rm=2'
  },
  {
    id: 4,
    name: 'Milk',
    price: 1.99,
    quantity: 0,
    image:
      'https://ts1.cn.mm.bing.net/th/id/R-C.6756b996ab96092a4af0b548197ce2d8?rik=uNEZ%2bUwmBXFj6Q&riu=http%3a%2f%2fpic.ntimg.cn%2f20110321%2f2531170_135216196710_2.jpg&ehk=Pm3%2f%2bUYFjSxXxJ8Xsm6bkkAh07CJm7J4BgwJjksOMTg%3d&risl=&pid=ImgRaw&r=0'
  },
  {
    id: 5,
    name: 'Bread',
    price: 1.5,
    quantity: 0,
    image:
      'https://ts3.cn.mm.bing.net/th?id=OIP-C.TZXl9sWf-mYfTBBt7kd-PQHaE8&w=306&h=204&c=8&rs=1&qlt=90&o=6&cb=13&dpr=1.6&pid=3.1&rm=2'
  },
  {
    id: 6,
    name: 'mango',
    price: 1.0,
    quantity: 0,
    image:
      'https://ts1.cn.mm.bing.net/th?id=OIP-C.1QWYfDmS82AmAUvI2Cc8rgD-D1&w=254&h=245&c=8&rs=1&qlt=90&o=6&cb=13&dpr=1.6&pid=3.1&rm=2'
  }
])

const cartItems = computed(()=>{
  return products.filter(product=>product.quantity>0)
})

const totalPrice = computed(()=>{
  return cartItems.value.reduce((total,item)=>{
    return total + (item.price * item.quantity)
  },0.0)
})

const Increment = (product)=>{
  product.quantity++
}
const Decrement = (product)=>{
  if(product.quantity > 0){
    product.quantity++
  }
}

const checkout = ()=>{
  alert('Checkout successful! Total price: $' + totalPrice.value);
  // 清空
  products.forEach(product => product.quantity = 0)
}
</script>
